
{-# OPTIONS -Wall #-}

module Typing where


data KSig = KSig deriving (Show)
data ASig = ASig deriving (Show)

ksig :: KSig
ksig = KSig

asig :: ASig
asig = ASig



instance Num KSig where
  (+) _ _ = ksig
  (-) _ _ = ksig
  (*) _ _ = ksig
  abs _   = ksig
  signum _      = ksig
  fromInteger _ = ksig

instance Fractional KSig where
  (/) _ _  = ksig
  fromRational _ = ksig

instance Num ASig where
  (+) _ _ = asig
  (-) _ _ = asig
  (*) _ _ = asig
  abs _   = asig
  signum _      = asig
  fromInteger _ = asig

instance Fractional ASig where
  (/) _ _  = asig
  fromRational _ = asig



type Time = Double


out :: ASig -> ()
out _ = ()

oscil_ar :: ASig -> ASig -> Int -> ASig
oscil_ar _ _ _ = asig


-- | Ochre uses @let@ for opcode assignment.
--
demo01 :: ()
demo01 = let a1 = oscil_ar 440.0 1.0 1 
         in out a1



linseg_kr :: Double -> [(Time,Double)] -> KSig
linseg_kr _ _ = ksig

-- | If we don\'t have I-Rate signals we only need one @upsample@.
--
upsample :: KSig -> ASig
upsample _ = asig


-- | Need to use @upsample@. 
--
-- Ideally Ochre will coalesce a1 and a2 to a single var use
-- with update. 
-- 
demo02 :: ()
demo02 = let kenv = linseg_kr 0.1 [(0.5,1.0), (1.0,0.1)]
             a1   = oscil_ar 440.0 1.0 1
             a2   = a1 * upsample kenv
         in out a2


type Name = String

data KRef = KRef Name deriving (Show)
data ARef = ARef Name deriving (Show)


-- The types of the reference operations follow SML.

makeRef_ar :: ASig -> ARef
makeRef_ar _ = ARef "<none>"

writeRef_ar :: ARef -> ASig -> ()
writeRef_ar _ _ = ()


readRef_ar :: ARef -> ASig
readRef_ar _ = asig
 
-- Note - probably need makeGlobal_ar to generate a @g@ name.


-- | A global instrument creating no audio, just a control 
-- signal.
--
demo03 :: ()
demo03 = let kenv       = linseg_kr 0.1 [(0.5,1.0), (1.0,0.1)]
             gr1        = makeRef_ar 0.0
         in writeRef_ar gr1 (upsample kenv)
             
